package com.potevio.simulator.strategy.impl;

import com.potevio.simulator.common.CommonFlag;
import com.potevio.simulator.common.GetHexMessage;
import com.potevio.simulator.config.MsgTypeConst;
import com.potevio.simulator.strategy.ReceiveMsgStrategy;
import com.potevio.simulator.task.CommonTask;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName Receive05
 * @Description: 接收05报文
 * @Date 2021/6/1  10:27
 * @Author zyd
 */
public class Receive05Command implements ReceiveMsgStrategy {

    private static final Logger LOGGER = LoggerFactory.getLogger(Receive05Command.class);
    private static String[] fieldsArr10 = new String[]{"chargingPile"};
    private static String[] fieldsArr6C = new String[]{"issueOrganizationSerialNumber"};
    private static String[] fieldsArr7A = new String[]{"cardNumber", "issueOrganizationSerialNumber", "transactionTime"};
    private static String[] fieldsArr39 = new String[]{"chargeStartSOC", "cumulativeChargeKwh", "cumulativeChargeAh", "cumulativeChargeTime", "startMeterDegress", "endMeterDegress"};
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static Date date = null;
    public static String chargePortTemp = "";

    @Override
    public void receiveMessage(ChannelHandlerContext ctx, Map<String, LinkedHashMap<String, String>> resultHexMap, String revertCommandCode) throws Exception {

        // 修改充电桩状态 结束充电
        String paramType = resultHexMap.get("05").get("paramType");
        if ("02".equals(paramType)) {
            new CommonTask().updateChargeState();
        }
        //用户id
        String userId = resultHexMap.get("05").get("userId");
        //指令序号
        String instructionsNumber = resultHexMap.get("05").get("instructionsNumber");


        String chargePort = resultHexMap.get("05").get("chargeModelLocalNumber");
        //输出功率
        String chargeModelControlParam = resultHexMap.get("05").get("chargeModelControlParam");


        Deal79Command.date = new Date();
        chargePortTemp = chargePort;
        date = new Date();
        // 回复15
        Map<String, LinkedHashMap<String, String>> map15 = new GetHexMessage().getDataDomain(MsgTypeConst.CHARGE_PILE_LONG_RANGE_CONTROL_REPLY);
        // 处理15报文
        map15.get("0x15").put("paramType", paramType);
        map15.get("0x15").put("chargeModelControlParam", chargeModelControlParam);
        map15.get("0x15").put("userId", userId);
        map15.get("0x15").put("instructionsNumber", instructionsNumber);
        if ("3.4a".equals(CommonFlag.version) || "3.4a1".equals(CommonFlag.version)) {
            map15.put("0x15", deal15(map15.get("0x15"), chargePort));
        }
        ctx.channel().writeAndFlush(map15);
    }

    /**
     * 处理15报文
     *
     * @param map
     * @param chargePort
     * @return
     */
    private LinkedHashMap<String, String> deal15(LinkedHashMap<String, String> map, String chargePort) {
        map.put("chargeModelLocalNumber", chargePort);
        //设置参数个数
        if ("3.4a1".equals(CommonFlag.version)) {
            map.put("chargeModelNumber", "01");
        }
        return map;


    }
}
